#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cstring>
using namespace std;

const int maxside = 4096;
const int maxside2 = maxside * maxside;
char prim[8][8];

struct DC
{
  char image[maxside][maxside];
  void clear()
  {
    memset( image, ' ', maxside2 );
  } 
};
DC dc1;
DC dc2;


void fill( int side, int base, DC& front, DC& back )
{
  if( side == base )
  {
    for( int i = 0; i < base; i++ )
      memcpy( front.image[i], prim[i], base );
  }
  else
  {
    int subside = side / base;
    fill( subside, base, back, front );
    front.clear();
    for( int u = 0; u < base; u++ )
    for( int v = 0; v < base; v++ )
    {
      if( prim[u][v] == ' ' ) continue;
      int oi = u * subside, oj = v * subside;
      for( int i = 0; i < subside; i++ )
        memcpy( front.image[i + oi] + oj, back.image[i], subside );
    }
  }
}

int main()
{
  while(1)
  {
    int n = 0;
    cin >> n;
    cin.get();  
    if( n == 0 ) break;
    for( int i = 0; i < n; i++ )
    {
      string line;
      getline( cin, line );
      strcpy( prim[i], line.c_str() );
    }
    int scale = 0;
    cin >> scale;
    
    int side = pow( n, scale );
    fill( side, n, dc1, dc2 );
    
    for( int i = 0; i < side; i++ )
    {
      cout.write( dc1.image[i], side );
      cout << endl;
    }
  }
  return 0;
}
